package xin.marcher.module.lifecycle.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.lifecycle.domain.dto.ProductBadIssuesResultDTO;
import xin.marcher.module.lifecycle.domain.request.ProductBadIssuesResultRequest;
import xin.marcher.module.lifecycle.service.ProductBadIssuesResultService;

/**
 *
 */
@Slf4j
@RestController
@RequestMapping("/badIssuesResult")
public class ProductBadIssuesResultController {

    @Autowired
    private ProductBadIssuesResultService productBadIssuesResultService;

    /**
     * 新增货品质量问题记录
     *
     * @param request
     * @return
     */
    @RequestMapping("/saveProductBadIssuesResult")
    public JsonResult<Long> saveProductBadIssuesResult(ProductBadIssuesResultRequest request) {
        try {
            Long count = productBadIssuesResultService.saveProductBadIssuesResult(request);
            return JsonResult.buildSuccess(count);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 修改货品质量问题记录
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateProductBadIssuesResult")
    public JsonResult<Boolean> updateProductBadIssuesResult(ProductBadIssuesResultRequest request) {
        try {
            Boolean result = productBadIssuesResultService.updateProductBadIssuesResult(request);
            return JsonResult.buildSuccess(result);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 查询货品质量问题记录
     *
     * @param request
     * @return
     */
    @RequestMapping("/getProductBadIssuesResult")
    public JsonResult<ProductBadIssuesResultDTO> getProductBadIssuesResult(ProductBadIssuesResultRequest request) {
        try {
            ProductBadIssuesResultDTO result = productBadIssuesResultService.getProductBadIssuesResult(request);
            return JsonResult.buildSuccess(result);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }


    /**
     * 分页查询货品质量问题记录
     *
     * @param request
     * @return
     */
    @RequestMapping("/queryProductBadIssuesResultByPage")
    public JsonResult<PageResult<ProductBadIssuesResultDTO>> queryProductBadIssuesResultByPage(ProductBadIssuesResultRequest request) {
        try {
            PageResult<ProductBadIssuesResultDTO> result = productBadIssuesResultService.queryProductBadIssuesResultByPage(request);
            return JsonResult.buildSuccess(result);
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

}
